<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use app\common\lib\Show;
use app\common\lib\StatusLib;
use think\facade\Request;
use think\facade\View;
use app\common\business\Goods as BusiGoods;

class Goods extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $data  = [];
        $title = Request::param('title', '', 'trim');
        $time  = Request::param('time', '', 'trim');
        if (!empty($title)) $data['title'] = $title;
        if (!empty($time)) $data['create_time'] = explode(' - ', $time);

        $busiGoods = new BusiGoods();
        $list      = $busiGoods->getLists($data);
        return View::fetch('', [
            'list' => $list,
            'deletestatus' => config('status.mysql.table_delete'),
            'title' => $title,
            'time'  => $time
        ]);
    }

    /**
     * 资源添加页面
     *
     * @return string
     */
    public function add()
    {
        return View::fetch();
    }

    /**
     * 商品添加
     *
     * @return \think\response\Json
     */
    public function save()
    {
        if (!Request::isPost()) {
            return Show::error('请求非法');
        }
        if (!Request::checkToken('__token__')) {
            return Show::error('token无效');
        }

        // 获取商品信息
        $goods = input('param.');
        $goods['category_path_id'] = $goods['category_id'];
        $idArr = explode(',', $goods['category_path_id']);
        $goods['category_id']  = end($idArr);

        // 商品数据验证
        $validate = new \app\admin\validate\Goods();
        if (!$validate->check($goods)) {
            return Show::error($validate->getError(), config('status.error'), ['token' => token()]);
        }

        try{
            $busiGoods = new BusiGoods();
            $res       = $busiGoods->add($goods);
        } catch (\Exception $e) {
            return Show::error($e->getMessage(), config('status.error'), ['token' => token()]);
        }

        return $res ? Show::success([], '商品添加成功') : Show::error('商品添加失败');
    }

    /**
     * 商品状态修改
     *
     * @return \think\response\Json
     */
    public function status()
    {
        $id = Request::param('id', 0, 'intval');
        $status = Request::param('status', 0, 'intval');

        if (!$id || !in_array($status, StatusLib::getTableStatus())) {
            return Show::error('参数错误');
        }

        try{
            $busiGoods = new BusiGoods();
            $res       = $busiGoods->updateStatusById($id, $status);
        } catch (\Exception $e) {
            return Show::error($e->getMessage());
        }

        return $res ? Show::success() : Show::error('状态修改失败');
    }

    /**
     * 商品排序修改
     *
     * @return \think\response\Json
     */
    public function listorder()
    {
        $id = Request::param('id', 0, 'intval');
        $listorder = Request::param('sort', 0, 'intval');

        if (!$id || !$listorder) {
            return Show::error('参数错误');
        }

        try{
            $busiGoods = new BusiGoods();
            $res       = $busiGoods->updateListorderById($id, $listorder);
        } catch (\Exception $e) {
            return Show::error($e->getMessage());
        }

        return $res ? Show::success() : Show::error('排序修改失败');
    }

    /**
     * 商品是否首页推荐修改
     *
     * @return \think\response\Json
     */
    public function is_index_recommend()
    {
        $id = Request::param('id', 0, 'intval');
        $is_index_recommend = Request::param('is_index_recommend', 0, 'intval');

        if (!$id || !in_array($is_index_recommend, [0, 1])) {
            return Show::error('参数错误');
        }

        try{
            $busiGoods = new BusiGoods();
            $res       = $busiGoods->updateIsIndexRecommentById($id, $is_index_recommend);
        } catch (\Exception $e) {
            return Show::error($e->getMessage());
        }

        return $res ? Show::success() : Show::error('首页推荐修改失败');
    }
}
